package com.david.andserver.util;


import com.david.core.utils.TextPinyinUtil;

import java.io.Serializable;

import androidx.annotation.Nullable;

public class Song implements Serializable, Comparable<Song> {
    public String name;//歌曲名
    public String singer;//歌手
    public String sizeStr;//歌曲所占空间大小,单位MB
    public long size;//歌曲所占空间大小
    public int duration;//歌曲时间长度
    public String path;//歌曲地址
    public long albumId;//图片id
    public String  albumImgPath;//专辑图片地址
//    public Bitmap albumImgBitmap;//专辑图片地址 Bitmap不能二进制保存到本地，所以在用到时在获取设置
    public long id;//歌曲id
    public char sortChar;//排序字符
    public String sortString;//排序拼音
    public Song() {
    }

    public Song(String name, String singer, String size, int duration, String path, long id) {
        this.name = name;
        this.singer = singer;
        this.sizeStr = size;
        this.duration = duration;
        this.path = path;
        this.id = id;
    }

    public long getAlbumId() {
        return albumId;
    }

    public void setAlbumId(long albumId) {
        this.albumId = albumId;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        char firstChar = name.charAt(0);
        if (TextPinyinUtil.isChinaChar(firstChar)) {
            sortString = (TextPinyinUtil.getInstance().convert(firstChar + "")).toUpperCase() + (name.length() > 1?((TextPinyinUtil.getInstance().convert(name.charAt(1) + "")) == null?"":TextPinyinUtil.getInstance().convert(name.charAt(1) + "")).toUpperCase():"");
            sortChar = sortString.charAt(0);
        } else if ((firstChar >= 65 && firstChar <= 90) || (firstChar >= 97 && firstChar <= 122)){//'A'-'Z' 'a'-'z'
            sortString = name.toUpperCase();
            sortChar = firstChar>=97? (char) (firstChar - 32) :firstChar;
        } else {
            sortChar = '#';
            sortString = name;
        }
    }

    public String getSinger() {
        return singer;
    }

    public void setSinger(String singer) {
        this.singer = singer;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
        sizeStr = size/1024/1024 + "MB";
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getAlbumImgPath() {
        return albumImgPath;
    }

    public void setAlbumImgPath(String albumImgPath) {
        this.albumImgPath = albumImgPath;
    }

    @Override
    public boolean equals(@Nullable Object obj) {
        if (obj instanceof Song) {
            Song tempSong = (Song) obj;
            return id == tempSong.id && name.equals(tempSong.name) && path.equals(tempSong.path) && singer.equals(tempSong.singer);
        }
        return false;
    }

    @Override
    public int compareTo(Song o) {
        if (this.equals(o)) {
            return 0;
        }

        if (sortChar == o.sortChar) {
            return o.sortString.equals(sortString)?name.compareTo(o.name):sortString.compareTo(o.sortString);
        } else {
            return (sortChar - o.sortChar) >= 1?1:-1;
        }
    }

    @Override
    public String toString() {
        return "{" +
                "name='" + name + '\'' +
                ", singer='" + singer + '\'' +
                ", sizeStr='" + sizeStr + '\'' +
                ", size=" + size +
                ", duration=" + duration +
                ", path='" + path + '\'' +
                ", albumId=" + albumId +
                ", albumImgPath='" + albumImgPath + '\'' +
                ", id=" + id +
                ", sortString=" + sortString +
                '}';
    }

    public String toJsonString(){
        return "{\"" +
                "name\":\"" + name + '\"' +
                ", \"singer\":\"" + singer + '\"' +
                ", \"sizeStr\":\"" + sizeStr + '\"' +
                ", \"size\":" + size + ""+
                ", \"duration\":" + duration + "" +
                ", \"path\":\"" + path + '\"' +
                ", \"id\":" + id + ""+
                ", \"sortString\":\"" + sortString + "\""+
                '}';
    }
}